/*
 *
 *      Copyright (c) 2018-2099, lipengjun All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the fly2you.cn developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lipengjun (939961241@qq.com)
 *
 */
package com.res.share.common.exception;

import com.res.share.common.WebResponse;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.thymeleaf.exceptions.TemplateInputException;

/**
 * 异常处理器
 *
 * @author 李鹏军
 */
@ControllerAdvice
public class BusinessExceptionHandler {
    //<editor-fold defaultstate="collapsed" desc="delombok">
    @SuppressWarnings("all")
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BusinessExceptionHandler.class);
    //</editor-fold>

    /**
     * 处理自定义异常
     *
     * @param e 自定义异常
     * @return restResponse
     */
    @ExceptionHandler(BusinessException.class)
    public ModelAndView handleBusinessException(Model model, BusinessException e) {
        //判断异常类型
        model.addAttribute("code",e.getCode());
        model.addAttribute("message",e.getMessage());
        return new ModelAndView("error/error");
    }

    /**
     * 路径不存在异常
     *
     * @param e 路径不存在异常
     * @return restResponse
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView handlerNoFoundException(Model model,Exception e) {
        log.error(e.getMessage(), e);
        model.addAttribute("code",400);
        model.addAttribute("message","路径不存在，请检查路径是否正确");
        return new ModelAndView("error/error");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public WebResponse handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return WebResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "数据已存在");
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Model model,Exception e) {
        log.error(e.getMessage(), e);
        model.addAttribute("code",500);
        model.addAttribute("message","服务器冒烟了，要不然你稍后再试试");
        return new ModelAndView("error/error");
    }

    /**
     * @description:处理由断言，IllegalArgumentException抛出得异常信息
     * @return java.lang.String
     */
    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public WebResponse handleArgError(IllegalArgumentException e) {
        return WebResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(TemplateInputException.class)
    public WebResponse templateInputException(TemplateInputException e) {
        return WebResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }
}
